
const { Result } = require("@/libs/result");
const arrayUtil = require("@/utils/array.util");
const dateUtil = require("@/utils/date.util");
const dbTableNameConstant = require("@/constants/db-table-name.constant");
const fileBusinessService = require("@/common/file-business.service");
const CommonService = require("@/common/common.service");
const inventoryModel = require("./inventory.model");

/**
 * 库存服务
 * @create_by Haor
 * @create_at 2025/07/24 16:38:36
 */
class InventoryService {
    /**
     * 创建库存 
     */
    async createInventory(data) {
        try {
            const [error, result] = await inventoryModel.createInventory(data); 
            if (error) {
                return Result.Failed('创建库存失败');
            }
            return Result.Success(result);
        } catch (error) {
            console.log('创建库存失败', error.message);
            return Result.Failed('创建库存失败');
        }
    }

    /**
     * 删除库存 
     */
    async deleteInventory(id) {
        try {
            const [error] = await inventoryModel.deleteInventory(id); 
            if (error) {
                return Result.Failed('删除库存失败');
            }
            return Result.Success();
        } catch (error) {
            console.log('删除库存失败', error.message);
            return Result.Failed('删除库存失败');
        }
    }

    /**
     * 修改库存 
     */
    async updateInventory(id, data) {
        try {
            const [error, result] = await inventoryModel.updateInventory({
                id: id,
                data: data,
                result: true
            }); 
            if (error) {
                return Result.Failed('修改库存失败');
            }
            return Result.Success(result);
        } catch (error) {
            console.log('修改库存失败', error.message);
            return Result.Failed('修改库存失败');
        }
    }

    /**
     * 查询库存列表
     */
    async queryInventoryList({ page, page_size, ...queryParams}) {
        try {
            const result = await inventoryModel.queryInventoryList({
                page: page || 1,
                page_size: page_size || 50,
                ...queryParams
            });
            if (result.code !== 200) {
                return Result.Failed('查询库存列表失败');
            }
            // 查询总条数
            const totalResult = await inventoryModel.getInventoryListTotal({ data: queryParams });

            return Result.Paging(page, totalResult.results[0].count, result.results || []);
        } catch (error) {
            console.log('查询库存列表失败', error.message);
            return Result.Failed('查询库存列表失败');
        }
    }

    /**
     * 获取库存
     */
    async getInventoryItem(id) {
        try {
            const [error, result] = await inventoryModel.getInventoryItem(id);
            if (error) {
                return Result.Failed('查询库存列表失败');
            }
            return Result.Success(result)
        } catch (error) {
            console.log('查询库存失败', error.message);
            return Result.Failed('查询库存失败');
        }
    }
}

module.exports = new InventoryService();

            